package com.etl.extract;

import com.etl.sys.entity.*;
import lombok.*;

import java.util.Objects;

/**
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2020-10-19 14:30
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@ToString
public class ScheduleTask {

    /**
     * 调度
     */
    private SysSchedule schedule;

    /**
     * 任务
     */
    private SysTask task;

    /**
     * 模块
     */
    private SysModule module;

    /**
     * 影院
     */
    private SysCinema cinema;

    /**
     * 租户
     */
    private SysTenant tenant;

    /**
     * 数据库，可能为空
     */
    private SysDatabase database;

    /**
     * 比较两个SysSchedule的属性是否相同
     *
     * @param otherSchedule other SysSchedule
     * @return boolean
     */
    public boolean compareSchedule(SysSchedule otherSchedule){
        if(Objects.isNull(schedule) || Objects.isNull(otherSchedule)){
            return false;
        }
        return Objects.equals(getScheduleProperties(schedule),
                getScheduleProperties(otherSchedule));
    }

    /**
     * 获取SysSchedule比较的属性
     *
     * @param srcSchedule src SysSchedule Object
     * @return new SysSchedule Object
     */
    private SysSchedule getScheduleProperties(SysSchedule srcSchedule) {
        SysSchedule newSchedule = new SysSchedule();
        newSchedule.setScheduleId(srcSchedule.getScheduleId());
        newSchedule.setCinemaId(srcSchedule.getCinemaId());
        newSchedule.setModuleId(srcSchedule.getModuleId());
        newSchedule.setDatabaseId(srcSchedule.getDatabaseId());
        newSchedule.setTaskId(srcSchedule.getTaskId());
        newSchedule.setTimeCron(srcSchedule.getTimeCron());
        newSchedule.setLimitNum(srcSchedule.getLimitNum());
        newSchedule.setExecTimeStart(srcSchedule.getExecTimeStart());
        newSchedule.setExecTimeEnd(srcSchedule.getExecTimeEnd());
        return newSchedule;
    }

    /**
     * 比较两个SysDatabase的属性是否相同
     *
     * @param otherDatabase other SysDatabase
     * @return boolean
     */
    public boolean compareDatabase(SysDatabase otherDatabase){
        if(Objects.isNull(database) || Objects.isNull(otherDatabase)){
            return false;
        }
        return Objects.equals(getDatabaseProperties(database),
                getDatabaseProperties(otherDatabase));
    }

    /**
     * 获取SysDatabase比较的属性
     *
     * @param srcDatabase src SysDatabase Object
     * @return new SysDatabase Object
     */
    private SysDatabase getDatabaseProperties(SysDatabase srcDatabase){
        SysDatabase newDatabase = new SysDatabase();
        newDatabase.setDatabaseId(srcDatabase.getDatabaseId());
        newDatabase.setJdbcUrl(srcDatabase.getJdbcUrl());
        newDatabase.setUsername(srcDatabase.getUsername());
        newDatabase.setPassword(srcDatabase.getPassword());
        return newDatabase;
    }
}
